//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  onLoad: function () {
  },
  
  getUserInfo: function(e) {
    var that = this;
    console.log(e)
    var userInfo = e.detail.userInfo
    //console.log("index2>getUserInfo>userInfo" + JSON.stringify(userInfo))
    var usrls = e.detail.userInfo
    var iv = e.detail.iv
    var encryptedData = e.detail.encryptedData
    //console.log("iv:" + iv);
    //console.log("encryptedData:" + encryptedData);
    app.globalData.userInfo = e.detail.userInfo
    console.log(e.detail.userInfo);
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
    // 登录
    wx.login({
      success: function (res) {
        //console.log("index2>code:" + res.code)
        //console.log("index2>iv:" + iv)
        //console.log("index2>encryptedData:" + encryptedData)
        //console.log("index2>userInfo:" + JSON.stringify(that.data.userInfo))
        if (res.code) {
          //发起网络请求
          wx.request({
            url: 'https://wwcxc.arksh.com/api/xcx/getWWUser',
            data: {
              code: res.code,
              iv: iv,
              encryptedData: encryptedData,
              nid: 35,
              userInfo: that.data.userInfo,
            },
            success: function (res) {
            console.log("index2>登录成功res：" + JSON.stringify(res));
              var ret = res.data.ret;
              //console.log("res.data:" + JSON.stringify(res.data));
              if (ret == 0) {
                //将用户信息存到缓存
                wx.setStorage({
                  key: "userInfo",
                  data: res.data
                })
                //跳转到页面index2
                console.log("index2>跳转到页面index")
                wx.reLaunch({
                  url: '../index/index'
                })
              }
            }
          })
        }
      }
    })
  }
})
